﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.Model;

public partial class Account_ChangePassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void CancelPushButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Default.aspx");
    }

    protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(CurrentPassword.Text.Trim()) || string.IsNullOrEmpty(NewPassword.Text.Trim()))
        {
            ErrorMessage.Text = "密码不能为空";
            return;
        }

        if (!NewPassword.Text.Trim().Equals(ConfirmNewPassword.Text.Trim()))
        {
            ErrorMessage.Text = "两次输入的新密码必须一致";
            return;
        }

        if (NewPassword.Text.Trim().Length < 4 || NewPassword.Text.Trim().Length > 16)
        {
            ErrorMessage.Text = "新密码要求长度为4~16位之间。";
            return;
        }

        string currentPassword = EmpolyeeDAL.GetPasswordByLoginName(Page.User.Identity.Name);

        if (!currentPassword.Equals(CurrentPassword.Text.Trim()))
        {
            var logger = log4net.LogManager.GetLogger("File");

            logger.Info(string.Format("用户{0}修改密码失败，原密码{1},新密码{2}", Page.User.Identity.Name, EmpolyeeDAL.GetPasswordByLoginName(Page.User.Identity.Name), CurrentPassword.Text.Trim())); 

            ErrorMessage.Text = "您输入的原有密码不正确，请重新输入";
            return;
        }

        if (EmpolyeeDAL.UpdatePasswordByLoginName(Page.User.Identity.Name, NewPassword.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript",
                                              "<script>ChangeOK();</script>");
        }
    }
}